<?=$this->header(true, true)?>


    <?=$this->menu("newsletters")?>


    <h1><?=$this->message("label.header", _Newsletter::BUNDLE)?></h1>


    <? /* Breadcrums route */ ?>
    <?=$this->route(
        array(
            array(
                "name"=>$this->message("label.header", _Newsletter::BUNDLE),
                "href"=>"newsletter/list"
            ),
            array(
                "name"=>$this->message("route.edit", _Core::BUNDLE) . " " . $this->message("label.recipients", _Newsletter::BUNDLE) . " " .
                    $this->message("label.newsletter", _Newsletter::BUNDLE). " \"".$this->newsletter($this->recipientForm->getId())->getSubject()."\""
            )
        )
    )?>


    <? /* Show error messages */ ?>
    <?=$this->errors(null, _Core::BUNDLE)?>


    <form id="<?=get_class($this->recipientForm)?>" action="newsletter/send" method="post">

        <div>
            <input type="hidden"
                   name="id"
                   value="<?=$this->recipientForm->getId()?>" />
            <input type="hidden"
                   name="<?=MVC::TOKEN_KEY?>"
                   value="<?=$this->request->getSession()->getAttribute(MVC::TRANSACTION_TOKEN_KEY)?>" />
        </div>

        <table cellspacing="0" cellpadding="0" style="width:100%;border-bottom:1px solid #efefef;">
        <tr>
            <td style="width:200px;vertical-align:top;">

                <fieldset style="border-bottom:none;border-right:1px solid #efefef;">

                    <legend><?=$this->message("label.filter.country", _Newsletter::BUNDLE)?></legend>

                    <? $countries = $this->newsletterCountries(); ?>

                    <? if(count($countries) > 0): ?>
                    <table cellspacing="0" cellpadding="0">
                    <tr>
                        <th colspan="2">
                            <a href="javascript:checkAll('<?=get_class($this->recipientForm)?>','countries')"><?=$this->message("choose.check-all", _Core::BUNDLE)?></a>
                            /
                            <a href="javascript:uncheckAll('<?=get_class($this->recipientForm)?>','countries')"><?=$this->message("choose.uncheck-all", _Core::BUNDLE)?></a>
                        </th>
                    </tr>
                    <?
                    $count = 0;
                    while(list($code, $name) = each($countries)): ?>

                        <tr class="ctrl<?=$this->error("countries")?>">

                            <th class="narrow">
                                <input name="countries[]"
                                       value="<?=$code?>"
                                       id="country_<?=$code?>"
                                       type="checkbox"
                                       class="check"
                                       <?=in_array($code, $this->recipientForm->getCountries()) ? " checked=\"checked\"" : ""?> />
                            </th>
                            <th><label for="country_<?=$code?>"><?=$name?></label></th>

                        </tr>

                    <? endwhile; ?>
                    </table>
                    <? endif; ?>


                </fieldset>

            </td>
            <td style="width:200px;vertical-align:top;">

                <fieldset style="border-bottom:none;border-right:1px solid #efefef;">

                    <legend><?=$this->message("label.filter.type", _Newsletter::BUNDLE)?></legend>

                    <? $types = $this->newsletterTypes(); ?>

                    <? if(count($types) > 0): ?>
                    <table cellspacing="0" cellpadding="0">
                    <tr>
                        <th colspan="2">
                            <a href="javascript:checkAll('<?=get_class($this->recipientForm)?>','types')"><?=$this->message("choose.check-all", _Core::BUNDLE)?></a>
                            /
                            <a href="javascript:uncheckAll('<?=get_class($this->recipientForm)?>','types')"><?=$this->message("choose.uncheck-all", _Core::BUNDLE)?></a>
                        </th>
                    </tr>
                    <?
                    $count = 0;
                    for($j=0; $j<count($types); $j++): ?>

                        <tr class="ctrl<?=$this->error("types")?>">

                            <th class="narrow">
                                <input name="types[]"
                                       value="<?=$types[$j]?>"
                                       id="type_<?=$types[$j]?>"
                                       type="checkbox"
                                       class="check"
                                       <?=in_array($types[$j], $this->recipientForm->getTypes()) ? " checked=\"checked\"" : ""?> />
                            </th>
                            <th><label for="type_<?=$types[$j]?>"><?=ucfirst($types[$j])?></label></th>

                        </tr>

                    <? endfor; ?>
                    </table>
                    <? endif; ?>

                </fieldset>

            </td>
            <td style="vertical-align:top;padding:30px 0px 40px 10px">
                <input type="button" value="Filter" class="button" onclick="this.form.action='newsletter/recipients/filter'; this.form.submit();" />
            </td>
        </tr>
        </table>

        <table cellspacing="0" cellpadding="0" style="width:100%;border-bottom:1px solid #efefef;">
        <tr>
            <td style="width:50%;vertical-align:top;">

                <fieldset style="border-bottom:none;border-right:1px solid #efefef;">

                <legend><?=$this->message("label.receive", _Newsletter::BUNDLE)?></legend>

                <? $recipients = $this->recipients($this->recipientForm, "NOT IN"); ?>

                <? if(count($recipients) > 0): ?>
                <table cellspacing="0" cellpadding="0">
                <tr>
                    <th colspan="2">
                        <a href="javascript:checkAll('<?=get_class($this->recipientForm)?>','recipients')"><?=$this->message("choose.check-all", _Core::BUNDLE)?></a>
                        /
                        <a href="javascript:uncheckAll('<?=get_class($this->recipientForm)?>','recipients')"><?=$this->message("choose.uncheck-all", _Core::BUNDLE)?></a>
                    </th>
                </tr>
                <?
                $count = 0;
                for($j=0; $j<count($recipients); $j++): ?>

                    <tr class="ctrl<?=$this->error("recipients")?>">

                        <th class="narrow">
                            <input name="recipients[]"
                                   value="<?=$recipients[$j]->getCustomerId()?>"
                                   id="recipient_<?=$recipients[$j]->getCustomerId()?>"
                                   type="checkbox"
                                   class="check"
                                   <?=in_array($recipients[$j]->getCustomerId(), $this->recipientForm->getRecipients()) ? " checked=\"checked\"" : ""?> />
                        </th>
                        <th><label for="recipient_<?=$recipients[$j]->getCustomerId()?>"><?=$recipients[$j]->getEmail()?></label></th>

                    </tr>

                <? endfor; ?>
                </table>
                <? endif; ?>

                </fieldset>

            </td>
            <td style="width:50%vertical-align:top;">

                <fieldset style="border-bottom:none;border-right:1px solid #efefef;">

                <legend><?=$this->message("label.received", _Newsletter::BUNDLE)?></legend>

                <? $recipients = $this->recipients($this->recipientForm, "IN"); ?>

                <? if(count($recipients) > 0): ?>
                <table cellspacing="0" cellpadding="0">
                <tr>
                    <th colspan="3">
                        <a href="javascript:checkAll('<?=get_class($this->recipientForm)?>','received')">
                            <?=$this->message("choose.check-all", _Core::BUNDLE)?>
                        </a> /
                        <a href="javascript:uncheckAll('<?=get_class($this->recipientForm)?>','received')">
                            <?=$this->message("choose.uncheck-all", _Core::BUNDLE)?>
                        </a>
                    </th>
                </tr>
                <?
                $count = 0;
                for($j=0; $j<count($recipients); $j++): ?>

                    <tr class="ctrl<?=$this->error("received")?>">
                        <th class="narrow">
                            <input name="received[]"
                                   value="<?=$recipients[$j]->getCustomerId()?>"
                                   id="received_<?=$recipients[$j]->getCustomerId()?>"
                                   type="checkbox"
                                   class="check" />
                        </th>
                        <th><label for="received_<?=$recipients[$j]->getCustomerId()?>"><?=$recipients[$j]->getEmail()?></label></th>
                    </tr>

                <? endfor; ?>
                </table>
                <? endif; ?>

                </fieldset>

            </td>
        </tr>
        </table>

            <div class="buttons" style="padding-left:20px">

                <input type="button"
                       onclick="to('newsletter/list')"
                       value="<?=$this->message("button.cancel", _Core::BUNDLE)?>"
                       class="button" />
                <input type="submit"
                       value="Send"
                       class="button" />

            </div>

    </form>

<?=$this->footer()?>